from Highlighter import Highlighter
from OutputBuilder import TestOutputBuilder

__author__ = 'deadmoroz'

import unittest

class IntegrationTests(unittest.TestCase):
    def setUp(self):
        self.highlighter = Highlighter()

    def testIni(self):
        ini = """
;Settings relating to the location and loading of the database
[Database]
ProfileDir=.
ShowProfileMgr=smart
Profile1_Name[] = "\|/_-=MegaDestoyer=-_\|/"
DefaultProfile=True

[AutoExec]
Use="prompt"
Glob=autoexec_*.ini
AskAboutIgnoredPlugins=0
"""
        result = """
<span class="comment">;Settings relating to the location and loading of the database</span>
<span class="title">[Database]</span>
<span class="setting">ProfileDir=<span class="value">.</span></span>
<span class="setting">ShowProfileMgr=<span class="value">smart</span></span>
<span class="setting">Profile1_Name[] = <span class="value"><span class="string">"\|/_-=MegaDestoyer=-_\|/"</span></span></span>
<span class="setting">DefaultProfile=<span class="value"><span class="keyword">True</span></span></span>

<span class="title">[AutoExec]</span>
<span class="setting">Use=<span class="value"><span class="string">"prompt"</span></span></span>
<span class="setting">Glob=<span class="value">autoexec_*.ini</span></span>
<span class="setting">AskAboutIgnoredPlugins=<span class="value"><span class="number">0</span></span></span>
"""
        builder = TestOutputBuilder()
        self.highlighter.highlight("ini", ini, builder)

        self.assertEquals(builder.get_content(), result)

    def testXml(self):
        xml = """<?xml version="1.0"?>
<response value="ok" xml:lang="en">
<text>Ok</text>
<comment html_allowed="true"/>
<ns1:description><![CDATA[
CDATA is <not> magical.
]]></ns1:description>
<a></a> <a/>
</response>
"""
        result = """<span class="pi"><?xml version="1.0"?></span>
<span class="tag"><<span class="title">response</span> <span class="attribute">value</span>=<span class="value">"ok"</span> <span class="attribute">xml:lang</span>=<span class="value">"en"</span>></span>
<span class="tag"><<span class="title">text</span>></span>Ok<span class="tag"></<span class="title">text</span>></span>
<span class="tag"><<span class="title">comment</span> <span class="attribute">html_allowed</span>=<span class="value">"true"</span>/></span>
<span class="tag"><<span class="title">ns1:description</span>></span><span class="cdata"><![CDATA[
CDATA is <not> magical.
]]></span><span class="tag"></<span class="title">ns1:description</span>></span>
<span class="tag"><<span class="title">a</span>></span><span class="tag"></<span class="title">a</span>></span> <span class="tag"><<span class="title">a</span>/></span>
<span class="tag"></<span class="title">response</span>></span>
"""
        builder = TestOutputBuilder()
        self.highlighter.highlight("xml", xml, builder)
        self.assertEquals(result, builder.get_content())

    def testJavascript(self):
        javascript = """function $initHighlight(block) {
if (block.className.search(/\\bno\-highlight\\b/) != -1)
return false;
try {
blockText(block);
} catch (e) {
if (e == 'Complex markup')
return;
}//try
var classes = block.className.split(/\s+/);
for (var i = 0 / 2; i < classes.length; i++) { // "0 / 2" should not be parsed as regexp start
if (LANGUAGES[classes[i]]) {
highlightLanguage(block, classes[i]);
return;
}//if
}//for
highlightAuto(block);
}//initHighlight
"""

        result = """<span class="function"><span class="keyword">function</span> <span class="title">$initHighlight</span><span class="params">(block)</span> {</span>
<span class="keyword">if</span> (block.className.search(<span class="regexp">/\\bno\-highlight\\b/</span>) != -<span class="number">1</span>)
<span class="keyword">return</span> <span class="literal">false</span>;
<span class="keyword">try</span> {
blockText(block);
} <span class="keyword">catch</span> (e) {
<span class="keyword">if</span> (e == <span class="string">'Complex markup'</span>)
<span class="keyword">return</span>;
}<span class="comment">//try</span>
<span class="keyword">var</span> classes = block.className.split(<span class="regexp">/\s+/</span>);
<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span> / <span class="number">2</span>; i < classes.length; i++) { <span class="comment">// "0 / 2" should not be parsed as regexp start</span>
<span class="keyword">if</span> (LANGUAGES[classes[i]]) {
highlightLanguage(block, classes[i]);
<span class="keyword">return</span>;
}<span class="comment">//if</span>
}<span class="comment">//for</span>
highlightAuto(block);
}<span class="comment">//initHighlight</span>
"""
        builder = TestOutputBuilder()
        self.highlighter.highlight("javascript", javascript, builder)
        self.assertEqual(result, builder.get_content())

    def testPython(self):
        python = """@requires_authorization
def somefunc(param1, param2):
r'''A docstring'''
if param1 > param2: # interesting
print 'Gre\\'ater'
print ''
return (param2 - param1 + 1) or None
class SomeClass: pass
"""
        result = """<span class="decorator">@requires_authorization</span>
<span class="function"><span class="keyword">def</span> <span class="title">somefunc</span><span class="params">(param1, param2)</span>:</span>
<span class="string">r'''A docstring'''</span>
<span class="keyword">if</span> param1 > param2: <span class="comment"># interesting</span>
<span class="keyword">print</span> <span class="string">'Gre\\'ater'</span>
<span class="keyword">print</span> <span class="string">''</span>
<span class="keyword">return</span> (param2 - param1 + <span class="number">1</span>) <span class="keyword">or</span> <span class="built_in">None</span>
<span class="class"><span class="keyword">class</span> <span class="title">SomeClass</span>:</span> <span class="keyword">pass</span>
"""
        builder = TestOutputBuilder()
        self.highlighter.highlight("python", python, builder)
        self.assertEqual(builder.get_content(), result)

    def testHtmlWithJavascriptAndCSS(self):
        html = """<!DOCTYPE html5>
<head>
<title>Title</title>

<style>
body {
width: 500px;
}
</style>

<script type="application/javascript">
function someFunction() {
return true;
}
</script>

<body>
<p class="something" id='12'>Something</p>
<p class=something>Something</p>
<!-- comment -->
<p class>Something</p>
<p class="something" title="p">Something</p>
</body>
"""
        result = """<span class="doctype"><!DOCTYPE html5></span>
<span class="tag"><<span class="title">head</span>></span>
<span class="tag"><<span class="title">title</span>></span>Title<span class="tag"></<span class="title">title</span>></span>

<span class="tag"><<span class="title">style</span>></span><span class="css">
<span class="tag">body</span> <span class="rules">{
<span class="rule"><span class="attribute">width</span>:<span class="value"> <span class="number">500</span>px</span>;</span>
<span class="rule">}</span></span>
</span><span class="tag"></<span class="title">style</span>></span>

<span class="tag"><<span class="title">script</span> <span class="attribute">type</span>=<span class="value">"application/javascript"</span>></span><span class="javascript">
<span class="function"><span class="keyword">function</span> <span class="title">someFunction</span><span class="params">()</span> {</span>
<span class="keyword">return</span> <span class="literal">true</span>;
}
</span><span class="tag"></<span class="title">script</span>></span>

<span class="tag"><<span class="title">body</span>></span>
<span class="tag"><<span class="title">p</span> <span class="attribute">class</span>=<span class="value">"something"</span> <span class="attribute">id</span>=<span class="value">'12'</span>></span>Something<span class="tag"></<span class="title">p</span>></span>
<span class="tag"><<span class="title">p</span> <span class="attribute">class</span>=<span class="value">something</span>></span>Something<span class="tag"></<span class="title">p</span>></span>
<span class="comment"><!-- comment --></span>
<span class="tag"><<span class="title">p</span> <span class="attribute">class</span>></span>Something<span class="tag"></<span class="title">p</span>></span>
<span class="tag"><<span class="title">p</span> <span class="attribute">class</span>=<span class="value">"something"</span> <span class="attribute">title</span>=<span class="value">"p"</span>></span>Something<span class="tag"></<span class="title">p</span>></span>
<span class="tag"></<span class="title">body</span>></span>
"""
        builder = TestOutputBuilder()
        self.highlighter.highlight("xml", html, builder)
        self.assertEqual(result, builder.get_content())

    def testCSS(self):
        css = """body,
html {
font: Tahoma, Arial, san-serif;
background: url('hatch.png');
}

@import url('print.css');

@media screen and (-webkit-min-device-pixel-ratio: 0) {
body:first-of-type pre::after {
content: 'highlight: ' attr(class);
}
}

@page:right {
margin: 1cm 2cm 1.3cm 4cm;
}

@font-face {
font-family: Chunkfive;
src: url('Chunkfive.otf');
}

#content {
width: /* wide enough */ 100% /* 400px */;
height: 100%
}

p[lang=ru] {
color: #F0F0F0; background: white !important;
}
"""
        result = """<span class="tag">body</span>,
<span class="tag">html</span> <span class="rules">{
<span class="rule"><span class="attribute">font</span>:<span class="value"> Tahoma, Arial, san-serif</span>;</span>
<span class="rule"><span class="attribute">background</span>:<span class="value"> <span class="function">url(<span class="string">'hatch.png'</span>)</span></span>;</span>
<span class="rule">}</span></span>

<span class="at_rule">@<span class="keyword">import</span> <span class="function">url(<span class="string">'print.css'</span>)</span></span>;

<span class="at_rule">@<span class="keyword">media</span> screen and (-webkit-min-device-pixel-ratio: <span class="number">0</span>) </span>{
<span class="tag">body</span><span class="pseudo">:first-of-type</span> <span class="tag">pre</span><span class="pseudo">::after</span> <span class="rules">{
<span class="rule"><span class="attribute">content</span>:<span class="value"> <span class="string">'highlight: '</span> <span class="function">attr(class)</span></span>;</span>
<span class="rule">}</span></span>
}

<span class="at_rule">@<span class="keyword">page</span></span><span class="pseudo">:right</span> <span class="rules">{
<span class="rule"><span class="attribute">margin</span>:<span class="value"> <span class="number">1</span>cm <span class="number">2</span>cm <span class="number">1.3</span>cm <span class="number">4</span>cm</span>;</span>
<span class="rule">}</span></span>

<span class="at_rule">@<span class="keyword">font-face</span></span> <span class="rules">{
<span class="rule"><span class="attribute">font-family</span>:<span class="value"> Chunkfive</span>;</span>
<span class="rule"><span class="attribute">src</span>:<span class="value"> <span class="function">url(<span class="string">'Chunkfive.otf'</span>)</span></span>;</span>
<span class="rule">}</span></span>

<span class="id">#content</span> <span class="rules">{
<span class="rule"><span class="attribute">width</span>:<span class="value"> <span class="comment">/* wide enough */</span> <span class="number">100</span>% <span class="comment">/* 400px */</span></span>;</span>
<span class="rule"><span class="attribute">height</span>:<span class="value"> <span class="number">100</span>%
</span>}</span></span>

<span class="tag">p</span><span class="attr_selector">[lang=ru]</span> <span class="rules">{
<span class="rule"><span class="attribute">color</span>:<span class="value"> <span class="hexcolor">#F0F0F0</span></span>;</span> <span class="rule"><span class="attribute">background</span>:<span class="value"> white <span class="important">!important</span></span>;</span>
<span class="rule">}</span></span>
"""
        builder = TestOutputBuilder()
        self.highlighter.highlight("css", css, builder)
        self.assertEqual(result, builder.get_content())


if __name__ == '__main__':
    unittest.main()
